%RBF_Network for Fold recognization using mthod 3 for 6 fold part of class1 part

clear;
clf;
close all;

%inputs P and targets T 
input1=load('..\data\composition_313.txt');
input2=load('..\data\secondary_313.txt');
input3=load('..\data\hydrophobicity_313.txt');
input4=load('..\data\polarity_313.txt');
input5=load('..\data\volume_313.txt');
input6=load('..\data\polarizability_313.txt');

Test_input1=load('..\data\composition_test.txt');
Test_input2=load('..\data\secondary_test.txt');
Test_input3=load('..\data\hydrophobicity_test.txt');
Test_input4=load('..\data\polarity_test.txt');
Test_input5=load('..\data\volume_test.txt');
Test_input6=load('..\data\polarizability_test.txt');

repeat_No=5;
Class_No=4;
Fold_No=27;

%the number of proteins in each fold for four classes  (for training set)
%N_Train = [13, 7, 12, 7, 9, 7, 30, 9, 16, 7, 8, 13, 8, 9, 9, 29, 11, 11, 13, 10, 9, 10, 11, 11, 7, 13, 14 ]; 
N_Train_1 = [13, 7, 12, 7, 9, 7]; 
N_Train_2 = [30, 9, 16, 7, 8, 13, 8, 9, 9];
N_Train_3 = [29, 11, 11, 13, 10, 9, 10, 11, 11];
N_Train_4 = [7, 13, 14]; 

N_Train_1_total = 55; 
N_Train_2_total = 109;
N_Train_3_total = 115;
N_Train_4_total = 34; 


%the number of proteins in each fold for four classes (for testing set)
%N_Test = [6, 9, 20, 8, 9, 9, 44, 12, 13, 6, 8, 19, 4, 4, 7, 48, 12, 13, 27, 12, 8, 14, 7, 4, 8, 27, 27 ];
N_Test_1 = [6, 9, 20, 8, 9, 9];
N_Test_2 = [44, 12, 13, 6, 8, 19, 4, 4, 7];
N_Test_3 = [48, 12, 13, 27, 12, 8, 14, 7, 4];
N_Test_4 = [8, 27, 27];

N_Test_1_total = 61; 
N_Test_2_total = 117;
N_Test_3_total = 145;
N_Test_4_total = 62; 

P1=input1(:,1:20)/100.;   %training input1
P2=input2(:,1:21)/100.;   %training input2
P3=input3(:,1:21)/100.;   %training input3
P4=input4(:,1:21)/100.;   %training input4
P5=input5(:,1:21)/100.;   %training input5
P6=input6(:,1:21)/100.;   %training input6
Total_input=[P1 P2 P3 P4 P5 P6];

Q1=Test_input1(:,1:20)/100.;   %testing input1
Q2=Test_input2(:,1:21)/100.;   %testing input2
Q3=Test_input3(:,1:21)/100.;   %testing input3
Q4=Test_input4(:,1:21)/100.;   %testing input4
Q5=Test_input5(:,1:21)/100.;   %testing input5
Q6=Test_input6(:,1:21)/100.;   %testing input6
Total_test_input=[Q1 Q2 Q3 Q4 Q5 Q6];

P11=Total_input(1:N_Train_1_total,:);
dummy=Total_input((N_Train_1_total+1):(N_Train_1_total+N_Train_2_total+N_Train_3_total+N_Train_4_total),:);

P=[];
for i=1:repeat_No
  P=[P;P11];
end
P=[P;dummy]';

Q11=Total_test_input(1:N_Test_1_total,:);
Q=Q11';

% determing the desired output, here this case is designed for recognizing fold_1~fold_6 in class1 part

m=0;

for k=1:repeat_No 
  for i=1:6     %  determing which class
    count=N_Train_1(i);    
    if i==1  
      for j=1:count,
        p1y(m+j,1)=1.;
        p1y(m+j,2)=0.;
        p1y(m+j,3)=0.;
        p1y(m+j,4)=0.;
        p1y(m+j,5)=0.;
        p1y(m+j,6)=0.;
        p1y(m+j,7)=0.;      %  for dummy fold
      end  
    elseif i==2 
      for j=1:count,
        p1y(m+j,1)=0.;
        p1y(m+j,2)=1.;
        p1y(m+j,3)=0.;
        p1y(m+j,4)=0.;       
        p1y(m+j,5)=0.;       
        p1y(m+j,6)=0.;             
        p1y(m+j,7)=0.;      %  for dummy fold        
      end
    elseif i==3 
      for j=1:count,
        p1y(m+j,1)=0.;
        p1y(m+j,2)=0.;
        p1y(m+j,3)=1.;
        p1y(m+j,4)=0.;       
        p1y(m+j,5)=0.;       
        p1y(m+j,6)=0.;             
        p1y(m+j,7)=0.;      %  for dummy fold        
      end
    elseif i==4
      for j=1:count,
        p1y(m+j,1)=0.;
        p1y(m+j,2)=0.;
        p1y(m+j,3)=0.;
        p1y(m+j,4)=1.;       
        p1y(m+j,5)=0.;       
        p1y(m+j,6)=0.;       
        p1y(m+j,7)=0.;      %  for dummy fold        
      end 
    elseif i==5
      for j=1:count,
        p1y(m+j,1)=0.;
        p1y(m+j,2)=0.;
        p1y(m+j,3)=0.;
        p1y(m+j,4)=0.;       
        p1y(m+j,5)=1.;       
        p1y(m+j,6)=0.;       
        p1y(m+j,7)=0.;      %  for dummy fold        
      end 
    else
      for j=1:count,
        p1y(m+j,1)=0.;
        p1y(m+j,2)=0.;
        p1y(m+j,3)=0.;
        p1y(m+j,4)=0.;       
        p1y(m+j,5)=0.;       
        p1y(m+j,6)=1.;       
        p1y(m+j,7)=0.;      %  for dummy fold        
      end 
    end
    m=m+count;  
  end
end

for i=1:(N_Train_2_total+N_Train_3_total+N_Train_4_total)
        p1y(m+i,1)=0.;
        p1y(m+i,2)=0.;
        p1y(m+i,3)=0.;
        p1y(m+i,4)=0.;       
        p1y(m+i,5)=0.;       
        p1y(m+i,6)=0.;       
        p1y(m+i,7)=1.;      %  for dummy fold
end


T1=p1y;
T=T1';


%Setting related parameters build a network and training 
 sse=27.25;  % goal
 dis=1.95; % spread
 mn=250;  % maximun allowed
 df=4;


delete brb61.txt;
delete trb61.txt;

net_rb61=newrb;
[net_rb61,tr]=newrb(P,T,sse,dis,mn,df);

%Training ...

%Simulate a neural network
trb61 = sim(net_rb61,Total_input');
brb61 = sim(net_rb61,Q);

save net_rb61; % save network parameters

save trb61;
save brb61;


% adjust the output to 0 and 1
% for training data

for j=1:313
   fid=fopen('trb61.txt','a');
   tall=trb61(:,j);
   ma1=max(tall);
   for k=1:7
      if tall(k) < ma1
         tall(k) = 0;
      else
         tall(k) = 1;
      end 
      fprintf(fid,'%d ',tall(k));
   end
   fprintf(fid,'\n');
end
fclose(fid);

%ending


%----------------------------------------------------------
% for testing data

for j=1:N_Test_1_total
   fid=fopen('brb61.txt','a');
   aall=brb61(:,j);
   ma=max(aall);
   for k=1:7
      if aall(k) < ma
         aall(k) = 0;
      else
         aall(k) = 1;
      end 
      fprintf(fid,'%d ',aall(k));
   end
   fprintf(fid,'\n');
end
fclose(fid);

%ending


% count correct number of testing results

load 'brb61.txt';

k1=0;
k2=0;
k3=0;
k4=0;
k5=0;
k6=0;
k7=0;
k8=0;
k9=0;
k_dummy=0;
index_dummy=[];

m=0;

for i=1:6
  count=N_Test_1(i);    
  if i==1
    for j=1:count
      if brb61(m+j,1) == 1
        k1=k1+1;
      end
      if brb61(m+j,7) == 1
        k_dummy=k_dummy+1;
        index_dummy=[index_dummy m+j];
      end
    end
  elseif i==2
    for j=1:count
      if brb61(m+j,2) ==1
        k2=k2+1;
      end
      if brb61(m+j,7) == 1
        k_dummy=k_dummy+1;
        index_dummy=[index_dummy m+j];        
      end      
    end
  elseif i==3
    for j=1:count  
      if brb61(m+j,3) ==1
        k3=k3+1;
      end
      if brb61(m+j,7) == 1
        k_dummy=k_dummy+1;
        index_dummy=[index_dummy m+j];        
      end      
    end
  elseif i==4
    for j=1:count  
      if brb61(m+j,4) ==1
        k4=k4+1;
      end
      if brb61(m+j,7) == 1
        k_dummy=k_dummy+1;
        index_dummy=[index_dummy m+j];        
      end          
    end
  elseif i==5
    for j=1:count  
      if brb61(m+j,5) ==1
        k5=k5+1;
      end
      if brb61(m+j,7) == 1
        k_dummy=k_dummy+1;
        index_dummy=[index_dummy m+j];        
      end      
    end
  else
    for j=1:count  
      if brb61(m+j,6) ==1
        k6=k6+1;
      end
      if brb61(m+j,7) == 1
        k_dummy=k_dummy+1;
        index_dummy=[index_dummy m+j];        
      end      
    end
  end
  m=m+count;  
end  

% display the result
% k1 k2 k3 k4 k5 k6 represent the correct number of each class 
% total :  the correct number of prediction accuracy of prediction=total/N_Test_1_total 

par=[sse dis]
total=k1+k2+k3+k4+k5+k6;
k=[k1 k2 k3 k4 k5 k6 k_dummy total]

%ending
%ending